package com.zslibrary.Tools;

import java.util.Arrays;

public class FrameBuilder {
    private byte[] mBuilder;
    private int headPtr=0;
    private int tailPtr=0;
    public FrameBuilder(int length){
        mBuilder=new byte[length];
    }

//    public FrameBuilder(ReceiverStruct receiver){
//        mBuilder=receiver.data;
//        tailPtr=receiver.size;
//    }

    public FrameBuilder(byte[] bytesData){
        mBuilder= Arrays.copyOf(bytesData,bytesData.length);
        tailPtr=bytesData.length;
    }

    public void append(byte byteData){
        mBuilder[tailPtr++]=byteData;
    }

    public void append(short shortData){
        mBuilder[tailPtr++]=(byte)((shortData&0xFFFF)>>8);
        mBuilder[tailPtr++]=(byte)(shortData&0xFF);
    }

    public void append(int intData){
        mBuilder[tailPtr++]=(byte)((intData&0xFFFFFFFF)>>24);
        mBuilder[tailPtr++]=(byte)((intData&0XFFFFFFFF)>>16);
        mBuilder[tailPtr++]=(byte)((intData&0xFFFFFFFF)>>8);
        mBuilder[tailPtr++]=(byte)(intData&0xFFFFFFFF);
    }

    public void append(byte[] bytesData){
        if(bytesData!=null){
            System.arraycopy(bytesData,0,mBuilder,tailPtr,bytesData.length);
            tailPtr+=bytesData.length;
        }
    }

    public void append(short[] shortData){
        if(shortData!=null){
            for(short d:shortData){
                mBuilder[tailPtr++]=(byte)((d&0xFFFF)>>8);
                mBuilder[tailPtr++]=(byte)(d&0xFF);
            }
        }
    }

    public void append(String strData){
        if((strData!=null)&&(strData.length()>0)){
            byte[] bytesDate=strData.getBytes();
            append(bytesDate);
        }
    }

    public int popData(int length){
        int ret;
        switch (length){
            case 1:
                ret=mBuilder[headPtr];
                break;

            case 2:
                ret=((mBuilder[headPtr]&0xFF)<<8)|(mBuilder[headPtr+1]&0xFF);
                break;

            case 4:
                ret=((mBuilder[headPtr]&0xFF)<<24)|((mBuilder[headPtr+1]&0xFF)<<16)|((mBuilder[headPtr+2]&0xFF)<<8)|(mBuilder[headPtr+3]&0xFF);
                break;

            default:
            ret=0;
            break;
        }
//        System.arraycopy(mBuilder,length,mBuilder,0,mBuilder.length-length);
        headPtr+=length;
        return ret;
    }

    /*public void popBytes(byte[] uData,int length){
        System.arraycopy(mBuilder,headPtr,uData,0,length);
        headPtr+=length;
    }*/

    public byte[] popBytes(int length){
        if(length>(mBuilder.length-headPtr))
            return null;
        byte[] returnData=new byte[length];
        System.arraycopy(mBuilder,headPtr,returnData,0,length);
        headPtr+=length;
        return returnData;
    }

    public void flushAll(){
        this.headPtr=0;
        this.tailPtr=0;
    }

    public byte getXOR(){
        byte XOR=0;
        for(int i=this.headPtr;i<this.tailPtr;i++){
            XOR^=mBuilder[i];
        }
        return XOR;
    }

    public int length(){
        return tailPtr-headPtr;
    }

    public byte[] toBytes(){
        return this.mBuilder;
    }
}
